package cn.bugstack.springframework.beans.factory.support;

import cn.bugstack.springframework.beans.factory.config.BeanDefinition;

import java.lang.reflect.Constructor;

/**
 * @Author: DRevelation
 * @Date: 2022/3/16 15:24
 * JDK实例化
 */
public class SimpleInstantiationStrategy implements InstantiationStrategy {
    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor ctor, Object[] args) throws Exception {
        Class beanClass = beanDefinition.getBeanClass();
        try {
            if (ctor != null) {
                beanClass.getDeclaredConstructor(ctor.getParameterTypes()).newInstance(args);
            } else {
                beanClass.getDeclaredConstructor().newInstance();
            }
        } catch (NoSuchMethodException e) {
            throw new Exception("Failed to instantiate [" + beanClass.getName() + "]");
        }
        return null;
    }
}
